package MyList;

public class MyList implements Operations{

    public MyListNode myListNode;

    public MyListNode headNode;//头结点

    @Override
    public void addFirst(int data) {

        myListNode = new MyListNode(data);

        if (this.headNode == null) {//判断是不是空链表
            this.headNode = myListNode;
            return;
        }

        myListNode.next = this.headNode;

        this.headNode = myListNode;

    }

    @Override
    public void addLast(int data) {

        myListNode = new MyListNode(data);

        if (this.headNode == null) {
            addFirst(data);
            return;
        }

        MyListNode curNode = this.headNode;

        while (curNode.next != null) {
            curNode = curNode.next;
        }

        curNode.next = myListNode;

    }

    @Override
    public void addIndex(int index, int data) {

        if (this.headNode == null && index != 0) {

            throw new EmptyListException("空链表只支持在下标为0的结点插入数据");

        } else if (index == 0) {

            addFirst(data);
            return;

        } else {

            MyListNode curNode = this.headNode;

            myListNode = new MyListNode(data);

            while (index - 1 != 0) {
                curNode = curNode.next;
                index--;
            }

            myListNode.next = curNode.next;
            curNode.next = myListNode;
        }


    }

    @Override
    public boolean contains(int key) {

        if (this.headNode == null) {
            throw new EmptyListException("链表为空");
        }

        MyListNode curNode = this.headNode;

        while (curNode != null) {
            if (curNode.val == key) {
                return true;
            }
            curNode = curNode.next;
        }

        return false;
    }

    @Override
    public void remove(int key) {

        if (this.headNode == null) {
            throw new EmptyListException("链表为空");
        }

        MyListNode curNode = this.headNode;

        while (curNode.next != null) {
            if (curNode.next.val == key) {
                curNode.next = curNode.next.next;
                return;
            }
            curNode = curNode.next;
        }

        if (this.headNode.val == key) {
            this.headNode = this.headNode.next;
        }

    }

    @Override
    public void removeAllKey(int key) {

        if (this.headNode == null) {
            throw new EmptyListException("链表为空");
        }

        MyListNode curNode = this.headNode.next;

        MyListNode preNode = this.headNode;

        while (curNode != null) {
            if (curNode.val == key) {
                preNode.next = curNode.next;
                curNode = curNode.next;
            } else {
                preNode = curNode;
                curNode = curNode.next;
            }
        }

        if (this.headNode.val == key) {
            this.headNode = this.headNode.next;
        }

    }

    @Override
    public int size() {
        int len = 0;

        MyListNode curNode = this.headNode;

        while (curNode != null) {
            len++;
            curNode = curNode.next;
        }

        return len;
    }

    @Override
    public void clear() {

       MyListNode preNode = this.headNode;

       while (this.headNode != null) {
           headNode = headNode.next;
           preNode = null;
           preNode = headNode;
       }
    }

    @Override
    public void display() {

        MyListNode curNode = this.headNode;

        while (curNode != null) {
            System.out.print(curNode.val + " ");

            curNode = curNode.next;
        }

        System.out.println();
    }
}
